import json
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType


"""
成本构成图：
-总收入： TOTAL_OPERATE_INCOME
-营业收入 OPERATE_INCOME
-总利润：TOTAL_PROFIT
-营业成本   OPERATE_COST
-研发费用   RESEARCH_EXPENSE
-营业税金及附加   OPERATE_TAX_ADD
-销售费用   SALE_EXPENSE
-管理费用    MANAGE_EXPENSE
"""

def get_test_data():
    with open("lirun_data.json","r") as fd:
        filedata = fd.read()
        json_data = json.loads(filedata)
        return json_data

def get_data(data, key):
    value = data.get(key)
    if value is None:
        value = 0
    return value

def make_xdata(data, fieldname):
    result = []
    count = data["result"]["count"]
    _data = data["result"]["data"]
    for _ in _data:
        result.append(get_data(_,"REPORT_DATE_NAME"))
    result.reverse()
    return result

def make_lirun_percentage(data, fieldname):
    result = []
    count = data["result"]["count"]
    _data = data["result"]["data"]
    for _ in _data:
        income = get_data(_,"OPERATE_INCOME")
        value = get_data(_, fieldname)
        percentage = value / income
        result.append({"value": percentage, "percent": percentage})
    result.reverse()    
    return result

def make_others_income(data):
    result = []
    count = data["result"]["count"]
    _data = data["result"]["data"]
    for _ in _data:
        income = get_data( _,"OPERATE_INCOME")
        cost = get_data( _,"OPERATE_COST")
        research = get_data( _,"RESEARCH_EXPENSE")
        tax = get_data(_,"OPERATE_TAX_ADD")
        sale = get_data(_,"SALE_EXPENSE")
        manage = get_data(_,"MANAGE_EXPENSE")
        other = income - cost - research - tax - sale - manage
        result.append({"value": other/income, "percent": other/income})
    result.reverse()    
    return result

def makechart_lirun(data_lirun):
    c = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
        .add_xaxis(make_xdata(data_lirun, "REPORT_DATE_NAME"))
        .add_yaxis("营业成本", make_lirun_percentage(data_lirun,"OPERATE_COST"), stack="stack1", category_gap="50%")
        .add_yaxis("研发费用", make_lirun_percentage(data_lirun,"RESEARCH_EXPENSE"), stack="stack1", category_gap="50%")
        .add_yaxis("营业税金及附加", make_lirun_percentage(data_lirun,"OPERATE_TAX_ADD"), stack="stack1", category_gap="50%")
        .add_yaxis("销售费用", make_lirun_percentage(data_lirun,"SALE_EXPENSE"), stack="stack1", category_gap="50%")
        .add_yaxis("管理费用", make_lirun_percentage(data_lirun,"MANAGE_EXPENSE"), stack="stack1", category_gap="50%")
        .add_yaxis("其他", make_others_income(data_lirun), stack="stack1", category_gap="50%")
        .add_yaxis("总利润", make_lirun_percentage(data_lirun,"TOTAL_PROFIT"), stack="stack1", category_gap="50%")
        .set_series_opts(
            label_opts=opts.LabelOpts(
                position="right",
                formatter=JsCode(
                    "function(x){return Number(x.data.percent * 100).toFixed() + '%';}"
                ),
            )
        )
        .render("利润表分析图.html")
    )

if __name__ == '__main__':
    data = get_test_data()
    makechart_lirun(data)